<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class UserController extends Controller {
    public function index(){
        echo "user";
//        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
    }

    public function showAction()
    {
        $param = $_GET['param'];
//        var_dump($param);exit;
        if(IS_GET && $param == '')
        {
            $this->display();
            return;
        }

        if($param)
        {
            $user = M('user');
            $userinfo = $user->field($param)->select();
//            var_dump($userinfo);exit;
            $info = '';
            foreach($userinfo as $k => $value){
                $info .= $value[$param].'<br>';
            }
            echo $info;
        }

    }

    public function loginAction(){

        $this->display();
    }
    public function model(){
//        $user = new Model('admin');
        $user = M('user');
//        $user = D('admin');
        var_dump($user->select());
        //查看缓存的字段
//        var_dump($user->getDbFields());
    }

    public function add_loginAction($name,$pass){
        $user = new M('user');
        $info = $user->where("username = '$name'")->select();
        if(!info)
        {
            $this->success('用户名错误!','/Home/User/login');
        }

        if($info['status'] == 1)
        {
            $this->success('用户被禁用!','/Home/User/login');
        }
        $ip = $_SERVER['REMOTE_ADDR'];
        $data = array('username'=>$name,'ip'=>$ip);
        $userinfo = new M('userinfo');
        $res = $userinfo->add($data);
        if(!$res)
        {
            $this->error('登录失败!','/Home/Usre/login');
        }

        //开启session存值
        session_start();
        if($_SESSION['username'] = $name)
        {
            $this->success('登陆成功!','/Home/index');
        }else{
            $this->error('登录失败','/Home/Usre/login');
        }
    }

    public function registerAction(){
        $uname = $_GET['uname'];
        if(IS_GET && $uname == '' )
        {
            $this->display();
            return;
        }
       if(IS_GET  && $uname)
       {
           $user = M('user');
           $info = $user->where("username = '$uname'")->find();
           if($info)
           {
               echo "账号已存在!";
           }else{
               echo "该账号可用!";
           }
       }

    }
    public function doregisterAction(){

        $name = $_POST['username'];
        $pass = $_POST['password'];
        $phone = $_POST['phone'];
        $time = $_SERVER['REQUEST_TIME'];
        $data = array('username'=>$name,'password'=>$pass,'phone'=>$phone,'add_time'=>$time);

        if(IS_POST){
            $user = M('user');
            $info  = $user->where("username = '$name'")->select();
            if(!$info)
            {
                $result = $user->add($data);
                if($result)
                {
                    $this->add_loginAction($name,$pass);

                }else{
                    $res['status'] = 0;
                    $res['content'] = '注册失败!';
                    $this->ajaxReturn($res);
                }
            }else{
                $res['status'] = 2;
                $res['content'] = '用户名已存在!';
                $this->ajaxReturn($res);
            }

        }else{
            $this->error('非法请求！');
        }
    }

    public function forgetAction(){
        $this->display();
    }

    public function doforgetAction(){
     
        if(!IS_POST)
        {
            $res['status'] = 0;
            $res['content'] = '请求方式不正确!';
            $this->ajaxReturn($res);
        }
        //接值
        $username = $_POST['username'];
        // var_dump($username);exit;
        $code = $_POST['code'];

        session_start();
        session('uname',$username);
        // $_code = session('code');
        // var_dump($_code);exit;
        //验证码对比
        
        $Verify = new \Think\Verify();
        if(!$Verify->check($code))
        {
            $res['status'] = 3;
            $res['content'] = '验证码不正确!';
            $this->ajaxReturn($res);
        }

        //验证账号是否存在
        $user = M('user');
        $data = $user->where('username =%u',array($username))->select();
        // var_dump($data);exit;
        if(empty($data))
        {
            $res['status'] = 2;
            $res['content'] = '用户不存在!';
            $this->ajaxReturn($res);
        }else{
            $this->success('用户验证成功', 'send');
        }
        
    }

    public function sendAction()
    {
        // var_dump($uname);
        $this->display();
    }

    public function mailAction()
    {
        // var_dump(11111);exit;
        if(!IS_POST)
        {
            $res['status'] = 0;
            $res['content'] = '请求方式不正确';
            $this->ajaxReturn($res);
        }
        //接值
        $username = $_POST['username'];
        $mail = $_POST['email'];
        // var_dump($mail);exit;
        $user = M('user');
        $email['email'] = $mail;
        $result = $user->where($email)->find();
        if(!$result)
        {
            $res['status'] = 0;
            $res['content'] = "邮箱尚未注册!";
            $this->ajaxReturn($res);
        }

        //控制URL链接的时效性
        $pwdToken = md5($result['id'],$result['username'],$result['password']);
        $link = "htttp://tp.com/Home/User/resetpwd/email/{$mail}/pwdToken/{$pwdToken}";
        $str = "您好!{$result['username']},请点击下面的链接重置您的密码:<p></p>".$link;
        $sendResult = $this->think_send_mail($mail,"TP重置密码",$str);
        if($sendResult['error'] == 1)
        {
            $res['status'] = 0;
            $res['content'] = "邮件发送失败!";
            $this->ajaxReturn($res);
        }else{
            $res['status'] = 1;
            $res['content'] ="系统已向您的邮箱发送了一封邮件,请登录邮箱点击链接修改密码!";
        }
        // var_dump($_uname);exit;
    }

    public function codeAction(){

        $Verify =     new \Think\Verify();
        $Verify->fontSize = 30;
        $Verify->length   = 4;
        $Verify->useNoise = false;
        $Verify->fontttf = '5.ttf';
        $Verify->useImgBg = true; 
        $Verify->entry();
        // $code = strtolower($Verify->getCode());
        // session('code',$code);
    }

    public function picAction()
    {
        if(IS_GET)
        {
            $this->display();
            return;
        }

        if(!IS_POST)
        {
            $res['status'] =0;
            $res['content'] = "请用正确的方式访问";
            $this->ajaxReturn($res);
        }

        $pic = $_FILES;
        var_dump($pic);exit;

    }
}